package cn.wolfcode.crm.service.imp;


import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.ProductSort;
import cn.wolfcode.crm.mapper.ProductMapper;
import cn.wolfcode.crm.mapper.ProductSortMapper;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.ProductPageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSortMapper productSortMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        if (id != null) {

        productMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void saveOrUpdate(Product record) {
        if (record.getId() != null) {
            //update
            productMapper.updateByPrimaryKey(record);
        }else {
            productMapper.insert(record);
            record.setProductSn(record.getId());
            productMapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public Product selectByPrimaryKey(Long id) {

            Product dept = productMapper.selectByPrimaryKey(id);
        return dept;

    }

    @Override
    public List<Product> selectAll() {
        List<Product> depts = productMapper.selectAll();
        return depts;
    }

    @Override
    public PageResult queryForList(QueryObject qo) {
        int rows = productMapper.rows(qo);
        if (rows==0) {
            return PageResult.EMPTY_RESULT;
        }
        ProductPageResult result =  new ProductPageResult();
        result.setTotal(rows);
        result.setRows(productMapper.queryForList(qo));
        result.setSort(productSortMapper.selectAll());
        return result;
    }
}
